package sg.edu.nus.iss.jass.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.FileInputStream;
import java.util.Properties;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.border.LineBorder;

import sg.edu.nus.iss.jass.controller.MainMenuController;

/**
 * @version 1.0
 * @author SE19 Team 1
 * 
 */
public class LoginWindow extends JFrame implements ActionListener {
	private static final long serialVersionUID = 1L;
	
	// Declaration of Global Variables.
	private Properties properties;
	private MainMenuController mainMenuController;
	private JButton btnAdvertiser1, btnAdvertiser2, btnExit;
	
	public LoginWindow(MainMenuController mainMenuController) {
		super("Job Advertisement and Search System (JASS) - Login");
		this.mainMenuController = mainMenuController;
		
		loadConfiguration();
		
		JPanel container = new JPanel();
		container.setLayout(new BorderLayout());
		container.add(headerPanel(), BorderLayout.NORTH);
		container.add(userSelectionPanel(), BorderLayout.CENTER);
		container.setBorder(new LineBorder(Color.BLACK));
		addWindowListener(windowListener);
		
		this.getContentPane().add(container);
		
		Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
		int x = (screen.width/2) - (StyleConstants.LOGIN_WINDOW_WIDTH/2);
		int y = (screen.height/2) - (StyleConstants.LOGIN_WINDOW_HEIGHT/2);
		this.setMinimumSize(new Dimension(StyleConstants.LOGIN_WINDOW_WIDTH, StyleConstants.LOGIN_WINDOW_HEIGHT));
		this.setBounds(x, y, StyleConstants.LOGIN_WINDOW_WIDTH, StyleConstants.LOGIN_WINDOW_HEIGHT);

		this.setUndecorated(true);
		this.setResizable(false);
		this.setVisible(true);
	}
	
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == btnAdvertiser1) {
			mainMenuController.setCurrentUser("12345", "Advertiser1");
			mainMenuController.displayMainMenu();
		}
		else if(e.getSource() == btnAdvertiser2) {
			mainMenuController.setCurrentUser("12345", "Advertiser2");
			mainMenuController.displayMainMenu();
		}
		else {
			mainMenuController.exitProgram();
		}
	}
	
	private void loadConfiguration() {
		try {
			properties = new Properties();
			FileInputStream in = new FileInputStream("properties/application.properties");
			properties.load(in);
			in.close();
		}
		catch(Exception ex) {
			ex.printStackTrace();
		}
	}
	
	private JPanel headerPanel() {
		JPanel aPanel = new JPanel();
		aPanel.setLayout(new GridLayout(2,1));
		
		JLabel lblHeader = new JLabel("<html><div align=\"center\">Welcome to Job Advertisement and Search System (JASS)<br>Produced By: Team SE19(Saturday)</div></html>");
		lblHeader.setFont(new Font("Calibri", Font.BOLD, 20));
		lblHeader.setHorizontalAlignment(SwingConstants.CENTER);
		aPanel.add(lblHeader);
		
		JLabel lblSelectMsg = new JLabel("Please select one of the users to continue.");
		lblSelectMsg.setFont(new Font("Calibri", Font.BOLD, 14));
		lblSelectMsg.setHorizontalAlignment(SwingConstants.CENTER);
		aPanel.add(lblSelectMsg);
		
		return aPanel;
	}
	
	private JPanel userSelectionPanel() {
		JPanel aPanel = new JPanel();
		aPanel.setLayout(new GridLayout(3,1,0,2));
		
		btnAdvertiser1 = new JButton(properties.getProperty("button.label.advertiser1"));
		btnAdvertiser1.setFont(new Font("Calibri", Font.BOLD, 18));
		btnAdvertiser1.addActionListener(this);
		aPanel.add(btnAdvertiser1);
		
		btnAdvertiser2 = new JButton(properties.getProperty("button.label.advertiser2"));
		btnAdvertiser2.setFont(new Font("Calibri", Font.BOLD, 18));
		btnAdvertiser2.addActionListener(this);
		aPanel.add(btnAdvertiser2);
		
		btnExit = new JButton(properties.getProperty("button.label.exitApplication"));
		btnExit.setFont(new Font("Calibri", Font.BOLD, 18));
		btnExit.addActionListener(this);
		aPanel.add(btnExit);
		
		return aPanel;
	}
	
	private WindowListener windowListener = new WindowAdapter () {
        public void windowClosing (WindowEvent e) {
        	mainMenuController.exitProgram();
        }
    };
}
